import javax.media.opengl.*;
import jocode.*;
import jomodel.*;
import processing.opengl.*;

// model will be loaded from OBJ file
JOModel object;
JOImage sky;
// light direction: 
//  if last value is 0, then this describes light direction
//  if 1, then light position
float lightDirection[]= { -2f, 2f, 2f, 0f };
// camera position
 float[] cameraPos = { 0f, 4f, 35f };
float cameraRotation = 0f;
float rotation = 0;

void setup() {
  size(800, 600, OPENGL);
  JOBridgeP5.init(this);
  
  sky = JOApp.loadImage("sky.jpg");

  // load the model
  object = new JOModel("JetFire/JetFire.obj");
  object.regenerateNormals();
}

// set the field of view and view depth
void setPerspective() {
    // select projection matrix (controls perspective)
    JOApp.gl.glMatrixMode(GL.GL_PROJECTION);
    JOApp.gl.glLoadIdentity();
    // fovy, aspect ratio, zNear, zFar
    JOApp.glu.gluPerspective(40f,                // zoom in or out of view
                             JOApp.aspectRatio,  // shape of viewport rectangle (width/height)
                             .1f,                // Min Z: how far from eye position does view start
                             500f);              // max Z: how far from eye position does view extend
    // return to modelview matrix
    JOApp.gl.glMatrixMode(GL.GL_MODELVIEW);
}

void draw() {
    // rotate 25 degrees per second
    rotation += 25f * (1f / frameRate);

    JOApp.begin();
    {
        // clear depth buffer and color
        JOApp.gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        // setup and enable perspective
        setPerspective();
        
        // set the lights
        JOApp.setLight(GL.GL_LIGHT1,
                       new float[] { 1f,  1f,  1f,  1f },    // diffuse color
                       new float[] { .6f, .6f, .9f, 1f },    // ambient
                       new float[] { 1f,  1f,  1f,  1f },    // specular
                       lightDirection);                      // direction/position

        JOApp.setAmbientLight(new float[] { .6f, .6f, .9f, 1f });
        
        // draw background image 
    	JOApp.drawImageFullScreen(sky);
    
        // adjust camera position according to arrow key events
        setCameraPosition();
       
        // select model view and reset
        JOApp.gl.glMatrixMode(GL.GL_MODELVIEW);
        JOApp.gl.glLoadIdentity();

        // set the viewpoint
        JOApp.glu.gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], // where is the eye
                            // look at a point directly in front of camera
                            cameraPos[0] - sin(radians(cameraRotation)), cameraPos[1], cameraPos[2] - cos(radians(cameraRotation)),
                            0f, 1f, 0f);   // which way is up

        // draw object at center
        JOApp.gl.glPushMatrix();
        {
            JOApp.gl.glRotatef(rotation, 0, 1, 0);  // turn it
            JOApp.gl.glScalef(20f,20f,20f);
            object.render();
        }
        JOApp.gl.glPopMatrix();

        // place the light, it will move with the rest of the scene
        JOApp.setLightPosition(GL.GL_LIGHT1, lightDirection);
    }
    JOApp.end();
}

public void setCameraPosition() {
    if (!keyPressed) return;
    
    if (key == CODED) {
        // turn left
        if (keyCode == LEFT) {
            cameraRotation += 1.0f;
        }
        // turn right
        else if (keyCode == RIGHT) {
            cameraRotation -= 1.0f;
        }
        // move forward in current direction
        else if (keyCode == UP) {
            cameraPos[0] -= sin(radians(cameraRotation)) * .3f;
            cameraPos[2] -= cos(radians(cameraRotation)) * .3f;
        }
        // move backward in current direction
        else if (keyCode == DOWN) {
            cameraPos[0] += sin(radians(cameraRotation)) * .3f;
            cameraPos[2] += cos(radians(cameraRotation)) * .3f;
        }
        // move camera down
        else if (keyCode == KeyEvent.VK_PAGE_UP) {
            cameraPos[1] +=  .13f;
        }
        // move camera up
        else if (keyCode == KeyEvent.VK_PAGE_DOWN) {
            cameraPos[1] -=  .13f;
        }
    }
}


